package com.yjy.user;

import com.alibaba.fastjson.JSON;
import com.yjy.redis.RedisUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisUtilsTest {
	
	@Autowired
	private RedisUtil redisUtil;

	// 测试插入zset数据
	@Test
	public void testZAdd() {
		List<Person> list = new ArrayList<>();
		for (int i = 1; i < 32; i++) {
			String day = String.format("%02d", i);
			list.add(new Person("name" + day, "2023-08-" + day, 1));
		}
		Map<String, Double> map = list.stream().collect(Collectors.toMap(JSON::toJSONString, item -> redisUtil.convertDateToRedisScore(item.getBirthday())));
		redisUtil.zAdd("person:testPersonKey", map);
	}

	// 测试通过分数获取zset数据
	@Test
	public void testZRangeByScore() {
		// 获取 2023-08-10 ~ 2023-08-20 的数据
		Double minScore = redisUtil.convertDateToRedisScore("2023-08-10");
		Double maxScore = redisUtil.convertDateToRedisScore("2023-08-20");
		Set<Object> objects = redisUtil.zRangeByScore("person:testPersonKey", minScore, maxScore);
		if (CollectionUtils.isEmpty(objects)) {
			return;
		}
		List<Person> list = objects.stream().map(item -> JSON.parseObject(item.toString(), Person.class)).sorted(Comparator.comparing(Person::getBirthday)).collect(Collectors.toList());
		for (Person person : list) {
			System.out.println(JSON.toJSONString(person));
		}
	}

	// 测试插入zset数据(不用转string类型)
	@Test
	public void testZAdd2() {
		List<Person> list = new ArrayList<>();
		for (int i = 1; i < 32; i++) {
			String day = String.format("%02d", i);
			list.add(new Person("name" + day, "2023-08-" + day, 1));
		}
		Map<Person, Double> map = list.stream().collect(Collectors.toMap(Function.identity(), item -> redisUtil.convertDateToRedisScore(item.getBirthday())));
		redisUtil.zAdd("person:testPersonKey2", map);
	}

	@Test
	public void testZRangeByScore2() {
		// 获取 2023-08-10 ~ 2023-08-20 的数据
		Double minScore = redisUtil.convertDateToRedisScore("2023-08-10");
		Double maxScore = redisUtil.convertDateToRedisScore("2023-08-20");
		LinkedHashSet<Person> objects = redisUtil.zRangeByScore("person:testPersonKey2", minScore, maxScore);
		if (CollectionUtils.isEmpty(objects)) {
			return;
		}
		for (Person person : objects) {
			System.out.println(person);
		}
	}

	// 测试通过分数获取zset数据
	@Test
	public void testZRange2() {
//		// 插入数据
//		redisUtil.zAdd("zSetValue","A",1);
//		redisUtil.zAdd("zSetValue","E",3);
//		redisUtil.zAdd("zSetValue","B",3);
//		redisUtil.zAdd("zSetValue","F",2);
//		redisUtil.zAdd("zSetValue","C",2);
//		redisUtil.zAdd("zSetValue","D",5);

		// [A, C, F, B, E, D]
		Set zSetValue = redisUtil.zRange("zSetValue",0,-1);
		System.out.println("通过range(K key, long start, long end)方法获取指定区间的元素:" + zSetValue);
		// [A, C, F]
		Set zSetValue2 = redisUtil.zRange("zSetValue",0,2);
		System.out.println("通过range(K key, long start, long end)方法获取指定区间的元素:" + zSetValue2);
	}
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	public static class Person {
		private String name;
		private String birthday;
		private Integer age;
	}
}
